/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.decode.ImageAttrs;
import me.panpf.sketch.display.TransitionImageDisplayer;
import me.panpf.sketch.process.RotateImageProcessor;
import me.panpf.sketch.request.CancelCause;
import me.panpf.sketch.request.DisplayListener;
import me.panpf.sketch.request.ErrorCause;
import me.panpf.sketch.request.ImageFrom;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.util.ResUtils;
import me.panpf.sketch.sample.widget.SampleImageView;
import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * RotateImageProcessorTestPageView
 */
public class RotateImageProcessorTestPageView extends AbstractPageView {

    private int mDegrees = 45;
    private boolean isApplyDone;
    private SketchImageView mImage_rotateFragment;

    public RotateImageProcessorTestPageView(Context Context, String name) {
        super(Context, name);
    }

    @Override
    public void initView() {
        setRootView(ResUtils.parseLayout(context, ResourceTable.Layout_rotate_image_processor_page_view));
        mImage_rotateFragment = (SketchImageView) getRootView().findComponentById(ResourceTable.Id_image_rotateFragment);
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        mImage_rotateFragment.getOptions().setMaxSize(attributes.width / 2, attributes.height / 2);
        getRootView().findComponentById(ResourceTable.Id_button_rotateFragment).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!isApplyDone) {
                    return;
                }
                mDegrees += 45;
                apply();
            }
        });
        mDegrees = 45;
        isApplyDone = true;
        mImage_rotateFragment.setDisplayListener(new DisplayListener() {
            @Override
            public void onStarted() {
                isApplyDone = false;
            }

            @Override
            public void onCompleted(Element drawable, ImageFrom imageFrom, ImageAttrs imageAttrs) {
                isApplyDone = true;
            }

            @Override
            public void onError(ErrorCause cause) {
                isApplyDone = true;
            }

            @Override
            public void onCanceled(CancelCause cause) {
                isApplyDone = true;
            }
        });
        apply();
    }

    private void apply() {
        if (!isApplyDone) {
            return;
        }
        isApplyDone = false;
        mImage_rotateFragment.getOptions().setProcessor(new RotateImageProcessor(mDegrees));
        Sketch.with(getContext()).displayFromResource(ResourceTable.Media_mei_nv, mImage_rotateFragment).commit();
    }
}
